package com.yvan.demo.proj2;

import com.yvan.Conv;
import com.yvan.http.HttpSession;
import com.yvan.http.YvanCookieJar;
import com.yvan.leto.*;
import com.yvan.leto.listener.LogbackListener;
import com.yvan.leto.listener.SpringBootListener;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@SpringBootApplication(exclude = {
        HibernateJpaAutoConfiguration.class,
        DataSourceAutoConfiguration.class
})
@Slf4j
public class StartProj2 {

    @SneakyThrows
    public static void main(String[] args) {
        LetoManage.addListener(new SpringBootListener());
        LetoManage.addListener(new LogbackListener());
//        LetoManage.addListener(new dataxConfig());
//        LetoManage.addListener(new gameConfig());
        LetoManage.init();


        log.info("init finished");

       if(LetoManage.isEnable()){
           log.info("logback content:" + Conv.NS(LetoManage.getSegmentContent(SegmentHelper.LOGBACK)).replace("\n", ""));
           log.info("jw content:" + LetoManage.getJsonWapper(SegmentHelper.SPRING_BOOT).toString(false));
        }

        log.info("baidu");
        HttpSession httpSession = new HttpSession(new YvanCookieJar());
        String body = httpSession.curl().get("http://www.baidu.com").execute().body().string();
        log.info("got " + body.length() + " bytes");
        SpringApplication application = new SpringApplication(StartProj2.class);
        application.run(args);
    }

//    static class gameConfig implements ConfigListener {
//        @Override
//        public String watchSegment() {
//            return "game";
//        }
//
//        @Override
//        @SneakyThrows
//        public void onChange(ConfigChangeEvent changeEvent) {
//            val prop = new Properties();
//            @Cleanup val inputStream = new ByteArrayInputStream(changeEvent.getChanges().getBytes("utf-8"));
//            prop.load(inputStream);
//            log.info(prop.toString());
//        }
//    }
//
//    static class dataxConfig implements ConfigListener {
//
//        @Override
//        public String watchSegment() {
//            return "datax";
//        }
//
//        @Override
//        public void onChange(ConfigChangeEvent changeEvent) {
//            JsonWapper jsonWapperDatax = new JsonWapper();
//            jsonWapperDatax.resetInnerMap(YvanUtil.readYaml(changeEvent.getChanges()));
//            log.info(jsonWapperDatax.toString(false));
//        }
//    }
}
